package com.fdd.furns.web;

import com.fdd.furns.pojo.Cart;
import com.fdd.furns.pojo.CartItem;
import com.fdd.furns.pojo.Furn;
import com.fdd.furns.service.FurnService;
import com.fdd.furns.service.impl.FurnServiceImpl;
import com.fdd.furns.utils.DataUtils;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;

@WebServlet(urlPatterns = {"/cartServlet"})
public class CartServlet extends BasicServlet{
    FurnService furnService = new FurnServiceImpl();

    protected void addItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //添加购物车1.获取家具 id
        int id = DataUtils.parseInt(req.getParameter("id"), 0);

        //通过id获取到家居信息，然后添加到购物车
        Furn furn = furnService.queryFurnById(id);
        if (furn == null) {
            return;
        }
        CartItem cartItem = new CartItem(furn.getId(), furn.getName(), 1, furn.getPrice(), furn.getPrice());

        //添加到购物车
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        if (cart == null) {
            cart = new Cart();
        }

        //判断库存是否足够
        
        if(furn.getStock() > cart.getItemCount(id)){
            cart.addItem(cartItem);
        }

        req.getSession().setAttribute("cart", cart);

        //重定向到购物车页面
        resp.sendRedirect(req.getHeader("Referer"));

    }

    protected void addItemByAjax(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //添加购物车1.获取家具 id
        int id = DataUtils.parseInt(req.getParameter("id"), 0);

        //通过id获取到家居信息，然后添加到购物车
        Furn furn = furnService.queryFurnById(id);
        if (furn == null) {
            return;
        }
        CartItem cartItem = new CartItem(furn.getId(), furn.getName(), 1, furn.getPrice(), furn.getPrice());

        //添加到购物车
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        if (cart == null) {
            cart = new Cart();
        }

        //判断库存是否足够

        if(furn.getStock() > cart.getItemCount(id)){
            cart.addItem(cartItem);
        }

        req.getSession().setAttribute("cart", cart);

        //重定向到购物车页面
//        resp.sendRedirect(req.getHeader("Referer"));

        //返回json数据
        HashMap<String, Object> resultMap = new HashMap<>();
        resultMap.put("totalCount", cart.getTotalCount());

        resp.getWriter().write(new Gson().toJson(resultMap));
    }

    protected void updateItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //更新购物车
        int id = DataUtils.parseInt(req.getParameter("id"), 0);
        int count = DataUtils.parseInt(req.getParameter("count"), 0);
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        if (cart != null) {
            cart.updateItem(id, count);

            resp.sendRedirect(req.getHeader("Referer"));
        }
    }
    //删除购物车中的家居项
    protected void deleteItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //删除购物车
        int id = DataUtils.parseInt(req.getParameter("id"), 0);
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        if (cart!= null) {
            cart.deleteItem(id);
            System.out.println("返回购物车页面");
            resp.sendRedirect(req.getHeader("Referer"));
        }
    }

    //清空购物车
    protected void clear(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        if (cart!= null) {
            cart.clear();
            //返回购物车页面
            resp.sendRedirect(req.getHeader("Referer"));
        }
    }

}
